package browser.dao;

import java.io.IOException;
import java.sql.*;
import javax.servlet.ServletOutputStream;

/**
 * Data access object that exports data to excel
 *
 */
public class ExportDataDao {

	public ExportDataDao(){
	}

	public void outDB(Connection conn, String query, ServletOutputStream sos) {



		 ResultSetMetaData rsmd = null;
		 Statement smt = null;
		 ResultSet rs = null;


		 try {
			 smt = conn.createStatement();
			 rs = smt.executeQuery(query);
			 rsmd = rs.getMetaData();


			 int colNum = rsmd.getColumnCount();
			 StringBuffer sbt = new StringBuffer();
			 for (int i = 1; i <= colNum; i++) {
				 sbt.append(rsmd.getColumnName(i) + "\t");
			 }
			 sos.println(sbt.toString());


			 while (rs.next()) {
				 StringBuffer sb = new StringBuffer();
				 for (int i = 1; i <= colNum; i++) {
					 sb.append(rs.getString(rsmd.getColumnName(i)) + "\t");
				 }
				 sos.println(sb.toString());
			 }
		 } catch (SQLException e) {
			 e.printStackTrace();
		 } catch (IOException e) {
			 e.printStackTrace();
		 }
	}
}
